package com.b2b.controller.company;

import com.b2b.model.entity.company.CompanyRole;
import com.b2b.model.receive.Staff;
import com.b2b.model.receive.StaffDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.exception.CustomException;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.CompanyRoleDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.util.CompanyRoleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/companyRole")
@PropertySource("classpath:config.properties")
public class CompanyRoleController {
    //    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;
    //    private final ICompanyRoleService companyRoleService;
//    @Autowired
//    public CompanyRoleController(ICompanyRoleService companyRoleService){
//        this.companyRoleService = companyRoleService;
//    }
    @Autowired
    CompanyRoleUtil companyRoleUtil;

    @PostMapping("/add")
    public ResponseBean add(@RequestBody CompanyRoleDto companyRoleDto){
        Integer count = companyRoleUtil.insertRole(companyRoleDto);
        if (count == -2){
            throw new CustomException("名称重复，插入失败(Insert Failure)");
        }
        if (count < 0 ){
            throw new CustomException("插入失败(Insert Failure)");
        }
        companyRoleDto.setRoleId(count);
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", companyRoleDto);
    }

    //    /**
//     * 新增Role权限
//     * @param companyRoleDto
//     * @return java.util.Map<java.lang.String,java.lang.Object>
//     * @author dolyw.com
//     * @date 2018/8/30 10:42
//     */
//    @PostMapping
////    @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
//    public ResponseBean add(@RequestBody CompanyRoleDto companyRoleDto) {
//        // 判断当前名称是否存在
//        CompanyRoleDto companyRoleDtoTemp = new CompanyRoleDto();
//        companyRoleDtoTemp.setRoleName(companyRoleDto.getRoleName());
//        companyRoleDtoTemp = companyRoleService.selectOne(companyRoleDtoTemp);
//        if (companyRoleDtoTemp != null ) {
//            throw new CustomUnauthorizedException("该API名称已存在(Account exist.)");
//        }
//        companyRoleDto.setRoleRouteauthorityList(JSON.toJSONString(companyRoleDto.getRoleRouteAuthorityList()));
//        companyRoleDto.setRoleAPIauthorityList(JSON.toJSONString(companyRoleDto.getRoleAPIAuthorityList()));
//        int count = companyRoleService.insert(companyRoleDto);
//        if (count <= 0) {
//            throw new CustomException("新增失败(Insert Failure)");
//        }
//        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", companyRoleDto);
//    }
//
    @GetMapping("/getMyEmployee")
    public ResponseBean getCompanyEmployee(StaffDto staffDto){
        if (staffDto.getPage() == null || staffDto.getRows() == null) {
            staffDto.setPage(1);
            staffDto.setRows(10);
        }
        PageHelper.startPage(staffDto.getPage(), staffDto.getRows());
        List<Staff> staffList = companyRoleUtil.getStaff(staffDto);
        PageInfo<Staff> selectPage = new PageInfo<Staff>(staffList);
        if (staffList == null) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);

    }

    @GetMapping("/getMy")
    public ResponseBean get(@RequestParam Integer companyId){
        List<CompanyRole> companyRoleList = companyRoleUtil.getByCompanyId(companyId);
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", companyRoleList);
    }



    /**
     * 获取公司API权限
     * @param
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
//    @GetMapping("/getMy")
//    //@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
//    public ResponseBean find(@RequestParam Integer companyId) {
//        List<CompanyRole> companyRoleList = companyRoleUtil.getById(companyId);
//        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", companyRoleList);
//    }

    @GetMapping("/byID")
    public ResponseBean findById(CompanyRoleDto companyRoleDto){
        CompanyRoleDto companyRoleDtoTemp = companyRoleUtil.getById(companyRoleDto);
        if (companyRoleDtoTemp.getRoleName() == null){
            throw new CustomUnauthorizedException("该角色不存在(Account not exist.)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Select Success)", companyRoleDtoTemp);
    }
    /**
     * 删除相关公司Role
     * @param companyRoleDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:43
     */
    @DeleteMapping("/delete")
    // @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean delete(CompanyRoleDto companyRoleDto) {
        Integer roleId = companyRoleDto.getRoleId();
        int count = companyRoleUtil.deleteCompanyRole(roleId);
        if (count == 2){
            throw new CustomException("删除失败，ID不存在(Deletion Failed. ID does not exist.)");
        }
        if (count <= 0) {
            throw new CustomException("删除失败");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }
    //
    @PutMapping("/updateRole")
    public ResponseBean updateUserRole(@RequestBody Map<String,Object> roleDto){
        int userId = Integer.parseInt(roleDto.get("userId").toString());
        int roleId = Integer.parseInt(roleDto.get("roleId").toString());
        Integer count = companyRoleUtil.updateUserRole(userId,roleId);
        if (count<=0){
            throw new CustomException("更新失败");
        }
        return new ResponseBean(HttpStatus.OK.value(), "修改用户角色成功!", null);
    }

//    @DeleteMapping("/delete")
//    public ResponseBean deleteRole(@RequestParam Integer roleId){
//        Integer count = companyRoleUtil.deleteUserRole(roleId);
//        if (count<=0){
//            throw new CustomException("删除失败");
//        }
//        return new ResponseBean(HttpStatus.OK.value(), "删除!", null);
//    }
    /**
     * 更新RoleAPIROUTE权限
     * @param companyRoleDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @PutMapping("/update")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean update( @RequestBody CompanyRoleDto companyRoleDto) {
        int count = companyRoleUtil.updateCompanyRole(companyRoleDto);
        if (count == -2) {
            throw new CustomException("更新失败,该角色不存在(Update Failure)");
        }
        if (count == -3){
            throw new CustomException("更新失败,角色名称重复(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", companyRoleDto);
    }
}
